﻿using System;
using System.IO;
using System.Diagnostics;

namespace tee
{
    internal class Program
    {
        private static int Main(string[] args)
        {
            Autoupdate();
            try
            {
                TextReader Input = Console.In;
                String Text = Input.ReadToEnd();
                Input.Close();
                if (args.Length > 0)
                    if ((args.Length > 1) && (Switchmatch(args[0], "a")))
                        File.AppendAllText(args[1], Text);
                    else
                        File.WriteAllText(args[0], Text);
                TextWriter Output = Console.Out;
                Output.Write(Text);
                Output.Close();
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 1;
            }
        }

        static private bool Switchmatch(String Argument, String Switch)
        {
            return (Argument.ToLower().Equals("-" + Switch.ToLower()) || Argument.ToLower().Equals("/" + Switch.ToLower()));
        }

        private static void Autoupdate()
        {
            String Params = "tee \"tee\" 1.1 Prompt DirectDL";
            //The rest is to remain
            String UpdaterFileName = @"Codeplex MSI Autoupdate.exe";
            String PathInProjectFolder = Path.Combine(@"..\..\..\Codeplex MSI Autoupdate\bin\release", UpdaterFileName);
            String PathInInstallFolder = Path.Combine(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]), UpdaterFileName);
            if (File.Exists(PathInInstallFolder))
                Process.Start(PathInInstallFolder, Params);
            else if (File.Exists(PathInProjectFolder))
                Process.Start(PathInProjectFolder, Params);
            else
                Console.WriteLine("Missing Codeplex MSI Autoupdate");
        }
    }
}